package com.example.bbs.controller;

import com.example.bbs.entity.Article;
import com.example.bbs.entity.DiseaseTag;
import com.example.bbs.entity.User;
import com.example.bbs.service.ArticleService;
import com.example.bbs.service.DiseaseTagService;
import com.example.bbs.util.CommentResolve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author jiajiazi
 * @version 1.0
 * @date 2021/5/5 20:15
 */
@Controller
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private DiseaseTagService diseaseTagService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentController commentController;

    //文章列表首页
    @RequestMapping
    public String defaultArticleIndex(Model model) {
        List<Article> allArts = articleService.getAllArts();
        List<DiseaseTag> diseaseTags = diseaseTagService.selectAllDisease();

        model.addAttribute("allArts", allArts);
        model.addAttribute("diseaseTags", diseaseTags);
        model.addAttribute("selectdiseaseTag", "0");
        return "article_list";
    }


    //文章管理页
    @RequestMapping("/index")
    public String ArticleManageIndex() {
        return "articlemanage";
    }

    //文章编辑页面（增加，修改）
    @RequestMapping("/toaddart")
    public String addArticleIndex(HttpServletRequest request, Model model) {
        model.addAttribute("user", (User) request.getSession().getAttribute("user"));
        model.addAttribute("diseasetags", diseaseTagService.selectAllDisease());
        return "article_add";

    }

    //文章添加
    @RequestMapping("/addart")
    @ResponseBody
    public String addArticle(@RequestBody Article article) {
        if (article == null) {
            return "500";
        }
        return articleService.addArticle(article);
    }

    // 查看谋篇
    @RequestMapping("/select_by_id/{id}")
    public String selectDiseaseById(@PathVariable("id") String id, Model model) {
        if (id == null) {
            model.addAttribute("article", null);
        } else {
            Article article = articleService.selectArticleById(Integer.parseInt(id));
            model.addAttribute("article", article);
            List<CommentResolve> commentResolves = commentController.commentsOfLevel1(Integer.parseInt(id));
            model.addAttribute("commentResolves", commentResolves);
        }
        return "article_detail";
    }

    //由tag查询
    @RequestMapping("/select_by_tagid/{id}")
    public String selectArtsByTag(@PathVariable("id") String tagid, Model model) {
        System.out.println(tagid);
        if (tagid == null || "".equals(tagid)) {
            return defaultArticleIndex(model);
        }
        List<Article> allArts = articleService.selectArtsByTag(Integer.parseInt(tagid));
        List<DiseaseTag> diseaseTags = diseaseTagService.selectAllDisease();

        model.addAttribute("allArts", allArts);
        model.addAttribute("diseaseTags", diseaseTags);
        model.addAttribute("selectdiseaseTag", tagid);
        return "article_list";
    }

    @RequestMapping("/update_support")
    @ResponseBody
    public String updateArtSupport(@RequestBody Map<String, String> map, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "500";
        }
        System.out.println(map.toString());
        return "200";
    }
}
